<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="调研时间" prop="surveyTime">
        <el-date-picker clearable v-model="queryParams.surveyTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择调研时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="调研地点" prop="deptId">
        <el-tree-select style="width: 100%;" v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
        value-key="id" class="margin-right" placeholder="请选择" check-strictly clearable  />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['coordination:survey:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:survey:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column label="序号" align="center" type="index" width="65" />
      <el-table-column label="调研时间" align="center" prop="surveyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.surveyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调研地点" align="center" prop="deptName" />
      <el-table-column label="调研领导" align="center" prop="leader" />
      <!-- <el-table-column label="调研目的" align="center" prop="surveyAim" /> -->
      <!-- <el-table-column label="调研发现问题" align="center" prop="question" /> -->
      <el-table-column label="调研图片" align="center" prop="surveyImg">
        <template #default="scope">
          <image-preview :src="scope.row.surveyImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="填表人" align="center" prop="reporterName" />
      <el-table-column label="联系电话" align="center" prop="phoneNum" />
      <el-table-column label="解决措施" align="center" prop="solution" />
      <el-table-column label="整改目标" align="center" prop="rectifyAim" />
      <el-table-column label="印证资料" align="center" prop="proof">
        <template #default="scope">
          <el-button link type="primary" @click.stop="previewFun(scope.row.proof)" v-if="scope.row.proof">查看</el-button> </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row,1)" >详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['coordination:survey:edit']">{{scope.row.status==1?'修改':"整改情况上报"}}</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['coordination:survey:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改调研对话框 -->
    <el-dialog :title="title" v-model="open" width="1200px" append-to-body >

      <div class="scroll">
      <el-form ref="surveyRef" :model="form" :rules="rules" label-width="100px">
        <div class="survey-title">调研信息</div>
        <el-row :gutter="20">
          <el-col :span="12" :xs="24">
            <el-form-item label="调研时间" prop="surveyTime">
              <el-date-picker style="width: 100%;" clearable v-model="form.surveyTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择调研时间" :disabled="deptId==form.source?false:true">
              </el-date-picker>
            </el-form-item>
          </el-col> <el-col :span="12" :xs="24">

            <el-form-item label="调研地点" prop="deptId">
              <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"  :disabled="deptId==form.source?false:true"
                value-key="id" class="margin-right" placeholder="请选择" check-strictly clearable  />
            </el-form-item>
          </el-col> <el-col :span="12" :xs="24">
            <el-form-item label="调研领导" prop="leader">
              <el-input v-model="form.leader"  placeholder="请输入内容" rows="6"  :disabled="deptId==form.source?false:true" />
            </el-form-item>
          </el-col> <el-col :span="24" :xs="24">
            <el-form-item label="调研目的" prop="surveyAim">
              <el-input v-model="form.surveyAim" type="textarea" placeholder="请输入内容" rows="6"  :disabled="deptId==form.source?false:true" />
            </el-form-item>
          </el-col> <el-col :span="24" :xs="24">
            <el-form-item label="调研发现问题" prop="question" label-width="100">
              <el-input v-model="form.question" type="textarea" placeholder="请输入内容" rows="6"   :disabled="deptId==form.source?false:true" />
            </el-form-item>

          </el-col> <el-col :span="12" :xs="24">
            <el-form-item label="整改时限" prop="deadLine">
              <el-date-picker style="width: 100%;" clearable v-model="form.deadLine" type="date" value-format="YYYY-MM-DD" placeholder="请选择整改时限"  :disabled="deptId==form.source?false:true">
              </el-date-picker>
            </el-form-item>
          </el-col> <el-col :span="12" :xs="24">
          </el-col> <el-col :span="12" :xs="24">
            <el-form-item label="调研图片" prop="surveyImg">
              <!-- :disabled="deptId==form.source?0:1" -->
              <ImageUpload  :modelValue="form.surveyImg" v-model="form.surveyImg" :disabled="isShow"  />
            </el-form-item>
          </el-col> 
          <el-col :span="12" :xs="24"></el-col>


          <el-col :span="24" :xs="24">
            <div class="survey-title" v-if="deptId!=101">整改信息</div>
          </el-col>


          <template v-if="(deptId!=101&&isEdit)||isShow">

             <el-col :span="12" :xs="24">
              <el-form-item label="印证资料" prop="proof">
                <FileUpload :modelValue="form.proof" v-model="form.proof" :disabled="isShow" />
              </el-form-item>
            </el-col> <el-col :span="24" :xs="24">
              <el-form-item label="解决措施" prop="solution">
                <el-input v-model="form.solution" type="textarea" placeholder="请输入内容"  :rows="6" :disabled="isShow"/>
              </el-form-item>

            </el-col> <el-col :span="24" :xs="24">
              <el-form-item label="整改目标" prop="rectifyAim">
                <el-input v-model="form.rectifyAim" type="textarea" placeholder="请输入内容" :rows="6" :disabled="isShow" />
              </el-form-item>

            </el-col> <el-col :span="24" :xs="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" :rows="6" :disabled="isShow" />
              </el-form-item>

            </el-col>
          </template>
        </el-row>
      </el-form>
    </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!isShow">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />
  </div>
</template>

<script setup name="Survey">
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey } from "@/api/coordination/survey";
import { getCAuditorUser } from "@/api/charge/cAuditor";

import ImageUpload from "@/components/ImageUpload";
import FileUpload from "@/components/FileUpload"
import { getCKInfo } from '@/utils/auth'
import { deptTreeSelect } from "@/api/system/user";
import Viewer from "@/components/Custom/viewer.vue";

const baseUrl = import.meta.env.VITE_APP_BASE_API;
const { proxy } = getCurrentInstance();
const dialogDoc = ref(false)
const fileViewerRef = ref()
const surveyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptId = ref(Number(getCKInfo('deptId')))
const deptOptions = ref([])
const userList = ref([])
const isEdit=ref(false)
const isShow=ref(false)
const data = reactive({
  form: {deptId: deptId.value,},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: deptId.value,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询调研列表 */
function getList() {
  loading.value = true;
  listSurvey(queryParams.value).then(response => {
    surveyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
}
function getUserList() {
  getCAuditorUser({}).then(response => {
    userList.value = response.data;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
  };
  proxy.resetForm("surveyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  form.value.source=deptId.value
  form.value.deptId=deptId.value
  isEdit.value=false;
  isShow.value=false;
  title.value = "添加调研";
  
}

/** 修改按钮操作 */
function handleUpdate(row,type) {
  reset();
  const _id = row.id || ids.value
  getSurvey(_id).then(response => {
    form.value = response.data;
    isEdit.value=true;
    open.value = true;
    if(type==1){
      isShow.value=true;
      title.value = "查看调研";
    }else{
      isShow.value=false;
      title.value = "修改调研";
    }
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["surveyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSurvey(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSurvey(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除调研编号为"' + _ids + '"的数据项？').then(function () {
    return delSurvey(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/survey/export', {
    ...queryParams.value
  }, `survey_${new Date().getTime()}.xlsx`)
}
// 预览
const previewFun = (file) => {
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data, '公文');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};

getList();
getDeptTree()
getUserList()
</script>

<style scoped>
.survey-title {
  color: #246bcc;
  font-weight: 600;
  font-size: 16px;
  position: relative;
  margin: 20px 0;
}
.survey-title::before {
  content: "";
  display: block;
  width: 2px;
  height: 10px;
  border-radius: 2px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.scroll{
  height:800px;
  overflow-y:scroll ;
  overflow-x:hidden ;
}
</style>
